home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1997 January / macformat46.iso / Shareware Plus / Developers / Library / Grant's CGI Framework / Grant's CGI Framework / grantscgi / Interface / WindowInt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-09-20  |  11.1 KB  |  568 lines

  1. /*****
  2.  *
  3.  *    WindowInt.c
  4.  *
  5.  *    This is a support file for "Grant's CGI Framework".
  6.  *    Please see the license agreement that accompanies the distribution package
  7.  *    for licensing details.
  8.  *
  9.  *    Copyright ©1995,1996 by Grant Neufeld
  10.  *    grant@acm.com
  11.  *    http://arpp.carleton.ca/cgi/framework/
  12.  *
  13.  *****/
  14.  
  15. #include "MyConfiguration.h"
  16. #if kCompileWithForeground
  17.  
  18. #include "globals.h"
  19.  
  20. #include "AboutBox.h"
  21. #include "CustomHandlers.h"
  22. #include "DebugUtil.h"
  23. #include "MemoryUtil.h"
  24. #include "MenuFunc.h"
  25. #include "ProcessUtil.h"
  26.  
  27. #include "WindowInt.h"
  28.  
  29.  
  30. /***  LOCAL PROTOTYPES ***/
  31.  
  32. static    Boolean        windowValid                ( WindowPtr );
  33. static    Boolean        windowHasScrollbars        ( WindowPtr );
  34. static    void        invalidateScrollbars    ( WindowPtr );
  35.  
  36.  
  37. /***  FUNCTIONS  ***/
  38.  
  39. /* MENU SELECTION HANDLING */
  40.  
  41. /*  */
  42. void
  43. WindowMenuClose ( short modifiers )
  44. {
  45.     WindowClose ( NULL, modifiers, true );
  46. } /* WindowMenuClose */
  47.  
  48.  
  49. /* EVENT HANDLING */
  50.  
  51. /* I think that state should be true if it is activating, false if deactivating.
  52.     IM-MTE: 2-53,54 */
  53. void
  54. WindowActivate ( WindowPtr theWindow, Boolean activate, const EventRecord *theEvent )
  55. {
  56. #pragma unused (theEvent)
  57.  
  58.     window_type        windType;
  59.     
  60.     windType = WindowType ( theWindow );
  61.     
  62.     /* restore or hide/reduce-to-outline window's selections */
  63.     switch ( windType )
  64.     {
  65.         #if kCompileWithApplicationWindows
  66.         case Window_Application :
  67.             CustomActivateWindow ( theWindow, activate );
  68.             break;
  69.         #endif
  70.         
  71.         case Window_dlgModal :
  72.         case Window_dlgMoveableModal :
  73.         case Window_about :
  74.         case Window_DA :
  75.         case Window_UNKNOWN :
  76.         default :
  77.             break;
  78.     }
  79.     
  80.     if ( activate )
  81.     {
  82.         /* show scroll bars if necessary */
  83.         
  84.         adjustMenus ();
  85.     }
  86.     else
  87.     {
  88.         /* hide scroll bars if necessary */
  89.     }
  90. } /* WindowActivate */
  91.  
  92.  
  93. /*  */
  94. void
  95. WindowUpdate ( WindowPtr theWindow )
  96. {
  97.     GrafPtr            oldPort;
  98.     window_type        windowType;
  99.     
  100.     windowType    = WindowType ( theWindow );
  101.     if ( !(windowType == Window_DA) )
  102.     {
  103.         GetPort        ( &oldPort );
  104.         SetPort        ( (GrafPtr)theWindow );
  105.         BeginUpdate    ( theWindow );
  106.  
  107.         switch ( windowType )
  108.         {
  109.             case Window_about :
  110.                 AboutBoxUpdate ();
  111.                 break;
  112.             
  113.             case Window_UNKNOWN :
  114.                 my_assert ( kForceAssert, "\pWindowUpdate: Unknown window type" );
  115.                 break;
  116.             
  117.             case Window_dlgModal :
  118.             case Window_dlgMoveableModal :
  119.             case Window_none :
  120.                 break;
  121.             
  122.             #if kCompileWithApplicationWindows
  123.             case Window_Application :
  124.             #endif
  125.             default :
  126.                 /* call custom window update procedure */
  127.                 #if kCompileWithApplicationWindows
  128.                 CustomUpdateWindow ( theWindow );
  129.                 #endif
  130.                 break;
  131.         }
  132.         
  133.         EndUpdate    ( theWindow );
  134.         SetPort        ( oldPort );
  135.     }
  136. } /* WindowUpdate */
  137.  
  138.  
  139. /* MOUSE CLICK HANDLING */
  140.  
  141. /* see the control manager chapter in IM-MTE for an example DoContentClick procedure
  142.     was doInContent */
  143. void 
  144. WindowContentClick ( WindowPtr theWindow, const EventRecord *eventPtr, Boolean *onItem )
  145. {
  146. #pragma unused (onItem)
  147.  
  148.     Point            whereClicked;
  149.     window_type        windowType;
  150.     GrafPtr            savePort;
  151.     
  152.     whereClicked = eventPtr->where;
  153.     
  154.     GetPort            ( &savePort );
  155.     SetPort            ( (GrafPtr)theWindow );
  156.     GlobalToLocal    ( &whereClicked );
  157.     SetPort            ( savePort );
  158.     
  159.     windowType = WindowType ( theWindow );
  160.     
  161.     switch ( windowType )
  162.     {
  163.         case Window_UNKNOWN :
  164.             my_assert ( kForceAssert, "\pWindowClose: unknown window type" );
  165.             break;
  166.         
  167.         case Window_about :
  168.         case Window_dlgModal :
  169.         case Window_dlgMoveableModal :
  170.         case Window_DA :
  171.             break;
  172.             
  173.         /* NOTE: unrecognized windows will be passed to the application
  174.             for clicks. */
  175.         #if kCompileWithApplicationWindows
  176.         case Window_Application :
  177.         #endif
  178.         default :
  179.             #if kCompileWithApplicationWindows
  180.             CustomClickInWindow ( theWindow, whereClicked, onItem );
  181.             #endif
  182.             break;
  183.     }
  184. } /* WindowContentClick */
  185.  
  186.  
  187. /* was doZoomBox */
  188. void
  189. WindowZoomBox ( WindowPtr theWindow, short partCode )
  190. {
  191.     GrafPtr    savePort;
  192.     
  193.     GetPort ( &savePort );
  194.     SetPort ( theWindow );
  195.  
  196.     EraseRect ( &theWindow->portRect );
  197.     ZoomWindow ( theWindow, partCode, false );
  198.  
  199.     invalidateScrollbars ( theWindow );
  200.     
  201.     /* setup for redraw */
  202.     InvalRect ( &theWindow->portRect );
  203.  
  204.     SetPort ( savePort );
  205. } /* WindowZoomBox */
  206.  
  207.  
  208. /* was doGrowWindow */
  209. void 
  210. WindowGrow ( WindowPtr theWindow, Point thePoint )
  211. {
  212.     switch ( WindowType( theWindow ) )
  213.     {
  214.         #if kCompileWithApplicationWindows
  215.         case Window_Application :
  216.             CustomGrowWindow ( theWindow, thePoint );
  217.             break;
  218.         #endif
  219.  
  220.         case Window_dlgModal :
  221.         case Window_dlgMoveableModal :
  222.         case Window_about :
  223.         case Window_DA :
  224.             break;
  225.         
  226.         case Window_UNKNOWN :
  227.         default :
  228.             WindowGrowHandler ( theWindow, thePoint );
  229.             break;
  230.     }
  231. } /* WindowGrow */
  232.  
  233.  
  234. /* This is where the actual growing is handled */
  235. void 
  236. WindowGrowHandler ( WindowPtr theWindow, Point thePoint )
  237. {
  238.     long    newSize;
  239.     Rect    limitRect;
  240.     
  241.     /* set maximum size of window to entire monitor(s) area */
  242.     limitRect        = gGrayRgnRect;
  243.     /* set minimum size of window */
  244.     limitRect.left    = kMinWindSize;
  245.     limitRect.top    = kMinWindSize;
  246.     
  247.     /* track user mouse and grow the window */
  248.     newSize = GrowWindow ( theWindow, thePoint, &limitRect );
  249.     if ( newSize != nil )
  250.     {
  251.         /* erase and invalidate scroll bar area */
  252.         invalidateScrollbars ( theWindow );
  253.         
  254.         SizeWindow ( theWindow, LoWord(newSize), HiWord(newSize), true );
  255.  
  256.         /* relocate scroll bars, then erase and ivalidate scroll bar area */
  257.         invalidateScrollbars ( theWindow );
  258.     }
  259. } /* WindowGrowHandler */
  260.  
  261.  
  262. /*  */
  263. #pragma mark -
  264.  
  265. /* was doCloseWindow */
  266. Boolean
  267. WindowClose ( WindowPtr theWindow, short modifiers, Boolean userInteract )
  268. {
  269.     WindowPtr    windowToClose;
  270.     Boolean        theWindowClosed;
  271.     #if kCompileWithApplicationWindows
  272.     OSErr        theErr;
  273.     #endif
  274.     
  275.     /* the window ptr must either be NULL or a valid window */
  276.     my_assert ( (theWindow == NULL) || (windowValid(theWindow)),
  277.         "\pWindowClose: invalid window" );
  278.     
  279.     /* assume function unsuccessful until a window is acutally closed */
  280.     theWindowClosed = false;
  281.     
  282.     if ( modifiers & optionKey )
  283.     {
  284.         /* if the optionKey is active, try to close all open windows */
  285.         theWindowClosed = WindowCloseAll ( userInteract );
  286.     }
  287.     else
  288.     {
  289.         if ( theWindow == NULL )
  290.         {
  291.             windowToClose = FrontWindow ();
  292.         }
  293.         else
  294.         {
  295.             windowToClose = theWindow;
  296.         }
  297.         
  298.         /* do the appropriate type of close based on the kind of window */
  299.         switch ( WindowType( windowToClose ) )
  300.         {
  301.             case Window_about :
  302.                 AboutBoxClose ();
  303.                 theWindowClosed = true;
  304.                 break;
  305.             
  306.             case Window_UNKNOWN :
  307.                 my_assert ( kForceAssert, "\pWindowClose: unknown window type" );
  308.                 break;
  309.             
  310.             case Window_dlgModal :
  311.             case Window_dlgMoveableModal :
  312.             case Window_DA :
  313.                 break;
  314.                 
  315.             /* NOTE: unrecognized windows will be passed to the application
  316.                 to close. */
  317.             #if kCompileWithApplicationWindows
  318.             case Window_Application :
  319.             #endif
  320.             default :
  321.                 #if kCompileWithApplicationWindows
  322.                 theErr = CustomCloseWindow ( windowToClose );
  323.                 theWindowClosed = (theErr == noErr);
  324.                 #endif
  325.                 break;
  326.         }
  327.     }
  328.     
  329.     return theWindowClosed;
  330. } /* WindowClose */
  331.  
  332.  
  333. /* was closeAllWindows */
  334. Boolean
  335. WindowCloseAll ( Boolean userInteract )
  336. {
  337.     Boolean        theWindowsClosed;
  338.     WindowPtr    theWindow;
  339.     
  340.     theWindowsClosed = true;
  341.     theWindow = FrontWindow ();
  342.     if ( theWindow != nil )
  343.     {
  344.         /* keep closing windows until they are all closed, or the user interrupts
  345.             or cancels the closing */
  346.         do
  347.         {
  348.             theWindowsClosed = WindowClose ( theWindow, nil, userInteract );
  349.             theWindow         = FrontWindow ();
  350.             
  351.             /* give other processes time */
  352.             ProcessGiveTime ( nil, true, NULL );
  353.         } while ( (theWindow != NULL) && (!userInteract || theWindowsClosed) );
  354.     }
  355.     
  356.     return theWindowsClosed;
  357. } /* WindowCloseAll */
  358.  
  359.  
  360. /* WINDOW KIND TESTING */
  361. #pragma mark -
  362.  
  363. /* was kindOfWindow */
  364. window_type
  365. WindowType ( WindowPtr theWindow )
  366. {
  367.     int                windowKind;
  368.     windowInfoHdl    windInfo;
  369.     
  370.     /* if the window is a null pointer return negative */
  371.     if ( theWindow == NULL )
  372.     {
  373.         return Window_none;
  374.     }
  375.     
  376.     windInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  377.     if ( windInfo != NULL )
  378.     {
  379.         return (*windInfo)->wType;
  380.     }
  381.     
  382.     windowKind = ((WindowPeek)theWindow)->windowKind;
  383.     if ( windowKind < nil )
  384.     {
  385.         /* desk accessory */
  386.         return Window_DA;
  387.     }
  388.     else if ( windowKind == dialogKind )
  389.     {
  390.         return Window_dlgModal;
  391.     }
  392.     else
  393.     {
  394.         return Window_UNKNOWN;
  395.     }
  396. } /* WindowType */
  397.  
  398.  
  399. /*  */
  400. Boolean
  401. WindowNewSetInfo (
  402.     WindowPtr        theWindow,
  403.     window_type        theType,
  404.     Boolean            hasScrollbars,
  405.     long            theData )
  406. {
  407.     windowInfoHdl    theWindInfo;
  408.     
  409.     theWindInfo = WindowNewInfoHdl ( theType, hasScrollbars, theData );
  410.     
  411.     WindowSetInfoHdl ( theWindow, theWindInfo );
  412.     
  413.     return ( theWindInfo != NULL );
  414. } /* WindowNewSetInfo */
  415.  
  416.  
  417. /*  */
  418. windowInfoHdl
  419. WindowNewInfoHdl (
  420.     window_type        theType,
  421.     Boolean            hasScrollbars,
  422.     long            theData )
  423. {
  424.     windowInfoHdl    theWindInfo;
  425.     
  426.     theWindInfo = (windowInfoHdl) MemoryNewHandle ( sizeof(windowInfo), NULL );
  427.     if ( theWindInfo != NULL )
  428.     {
  429.         (*theWindInfo)->wType        = theType;
  430.         (*theWindInfo)->scrollbars    = hasScrollbars;
  431.         (*theWindInfo)->data        = theData;
  432.     }
  433.     
  434.     return theWindInfo;
  435. } /* WindowNewInfoHdl */
  436.  
  437.  
  438. /*  */
  439. void
  440. WindowSetInfoHdl ( WindowPtr theWindow, windowInfoHdl theWindInfo )
  441. {
  442.     SetWRefCon ( theWindow, (long)theWindInfo );
  443. } /* WindowSetInfoHdl */
  444.  
  445.  
  446. /*  */
  447. windowInfoHdl
  448. WindowGetInfoHdl ( WindowPtr theWindow )
  449. {
  450.     windowInfoHdl    theWindInfo;
  451.     
  452.     theWindInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  453.     
  454.     return theWindInfo;
  455. } /* WindowGetInfoHdl */
  456.  
  457.  
  458. /* Return the user specified data from the windowInfoHdl of the window */
  459. long
  460. WindowGetData ( WindowPtr theWindow )
  461. {
  462.     windowInfoHdl    theWindInfo;
  463.     
  464.     theWindInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  465.     if ( theWindInfo == NULL )
  466.     {
  467.         return nil;
  468.     }
  469.     else
  470.     {
  471.         return (*theWindInfo)->data;
  472.     }
  473. } /* WindowGetData */
  474.  
  475.  
  476. /* Return the user specified data from the windowInfoHdl of the window, as a handle
  477.     For backwards compatibility. Avoid using this function. */
  478. Handle
  479. WindowGetDataHdl ( WindowPtr theWindow )
  480. {
  481.     return (Handle) (WindowGetData(theWindow));
  482. } /* WindowGetDataHdl */
  483.  
  484.  
  485. /*  */
  486. void
  487. WindowDisposeInfoHdl ( WindowPtr theWindow )
  488. {
  489.     Handle    theWindInfo;
  490.     
  491.     theWindInfo = (Handle) GetWRefCon ( theWindow );
  492.     if ( theWindInfo != NULL )
  493.     {
  494.         DisposeHandle ( theWindInfo );
  495.     }
  496. } /* WindowDisposeInfoHdl */
  497.  
  498.  
  499. /* MISC UTILITIES */
  500.  
  501. /*  */
  502. static Boolean
  503. windowValid ( WindowPtr theWindow )
  504. {
  505.     Boolean        isValid;
  506.     
  507.     if ( theWindow == NULL )
  508.     {
  509.         isValid = false;
  510.     }
  511.     else
  512.     {
  513.         /* need more tests here */
  514.         isValid = true;
  515.     }
  516.     
  517.     return isValid;
  518. } /* windowValid */
  519.  
  520.  
  521. /*  */
  522. static Boolean
  523. windowHasScrollbars ( WindowPtr theWindow )
  524. {
  525.     windowInfoHdl    windInfo;
  526.     
  527.     windInfo = (windowInfoHdl) GetWRefCon ( theWindow );
  528.     if ( windInfo != NULL )
  529.     {
  530.         return (*windInfo)->scrollbars;
  531.     }
  532.     else
  533.     {
  534.         return false;
  535.     }
  536. } /* windowHasScrollbars */
  537.  
  538.  
  539. /* Invalidate the graphic region encompasing the scrollbars so that they will be
  540.     properly updated. */
  541. static void
  542. invalidateScrollbars ( WindowPtr theWindow )
  543. {
  544.     Rect    tempRect;
  545.     
  546.     if ( windowHasScrollbars(theWindow) )
  547.     {
  548.         SetPort ( theWindow );
  549.         
  550.         /* invalidate & erase vertical (right-hand side) scrollbar */
  551.         tempRect        = theWindow->portRect;
  552.         tempRect.left    = tempRect.right-15;
  553.         InvalRect ( &tempRect );
  554.         EraseRect ( &tempRect );
  555.         
  556.         /* invalidate & erase horizontal (bottom) scrollbar */
  557.         tempRect        = theWindow->portRect;
  558.         tempRect.top    = tempRect.bottom-15;
  559.         InvalRect ( &tempRect );
  560.         EraseRect ( &tempRect );
  561.     }
  562. } /* invalidateScrollbars */
  563.  
  564.  
  565. #endif    /* kCompileWithForeground */
  566.  
  567. /*****  EOF  *****/
  568.